import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

const state = {
  count : 1
}
//同步
const mutations = {
  add(state,obj){
    let { n=1,txt=''} = obj
    console.log('执行add',txt)
    state.count += n
  },
  reduce(state,n=1){
    console.log('执行reduce')
    state.count -= n
  }
}

const getters = {
  count:function(state){
    return state.count += 2
  }
}
//异步
const actions = {
  addAction(context,txt){
    context.commit('add',{n:5,txt})
  },
  reduceAction({commit}){
    commit('reduce')
  }
}
export default new Vuex.Store({
  state,mutations,getters,actions
})